﻿using Ivory;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Paintball.Interfaces;
using Paintball.Objects;
using Paintball.Libaries;
using Microsoft.Xna.Framework.Input;

namespace Paintball
{
    public class GameManager : IDrawableObject
    {
        //Fields

        private Map GameMap;
        private IEnumerable<Player> PlayerList;

        //Construct
        public GameManager(string username)
        {

        }

        //Methods
        public void Draw(SpriteBatch batch, GameTime time)
        {
            GameMap.Draw(batch, time);

            foreach (Sprite m in GameMap.GetObjects().Concat(PlayerList).OrderBy(o => o.LayerIndex))
                m.Draw(batch, time);
        }
        public void Update(InputControl input, GameTime time)
        {
            //Check this Player Input
            Vector2 MoveDir = Vector2.Zero;

            if (input.IsKeyDown(Keys.W) || input.IsKeyDown(Keys.Up))
                MoveDir += new Vector2(0, -1);
            else if (input.IsKeyDown(Keys.S) || input.IsKeyDown(Keys.Down))
                MoveDir += new Vector2(0, 1);

            if (input.IsKeyDown(Keys.A) || input.IsKeyDown(Keys.Left))
                MoveDir += new Vector2(-1, 0);
            else if (input.IsKeyDown(Keys.D) || input.IsKeyDown(Keys.Right))
                MoveDir += new Vector2(1, 0);

            if (MoveDir != Vector2.Zero)
            {
                //Send the new position to the server
                //Send(new PBTActionMessage(_MeAsClient, 0));
            }
        }
        public bool Load()
        {
            List<MapObject> objs = new List<MapObject>();

            for (int i = 0, x = 360, y = 1600; i < 10; i++, y += 128)
                objs.Add(new Barrel(new Rectangle(x, y, 128, 128)));

            objs.Add(new Tree(new Rectangle(810, 340, 128, 128)));
            objs.Add(new Tree(new Rectangle(1020, 490, 128, 128)));
            objs.Add(new Tree(new Rectangle(840, 645, 128, 128)));
            objs.Add(new Tree(new Rectangle(1110, 710, 128, 128)));
            objs.Add(new Tree(new Rectangle(810, 340, 128, 128)));
            objs.Add(new Tree(new Rectangle(810, 340, 128, 128)));

            GameMap = new Map(objs);
            PlayerList = new List<Player>();

            return true;
        }
                
    }
}
